﻿using System;

namespace WindowsAPI.Service
{
    internal class UsingObject<T> : IDisposable
    {
        private readonly T f_Obj;
        private readonly Action<T> f_Disposer;

        public T Object { get { return f_Obj; } }

        public UsingObject(T obj, Action<T> Disposer)
        {
            if(obj == null)
                throw new ArgumentNullException(nameof(obj));
            if(Disposer == null)
                throw new ArgumentNullException(nameof(Disposer));

            f_Obj = obj;
            f_Disposer = Disposer;
        }


        public void Dispose() { f_Disposer(f_Obj); }

        public static implicit operator T(UsingObject<T> obj) { return obj.f_Obj; }
    }
}
